package com.chuangyang.fixboxmaster.ui.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.NewOrder;

import java.util.ArrayList;

/**
 * Created by vanshu on 15/4/9 16:22.
 */
public class HomeListAdapter extends BaseAdapter {
    private ArrayList<NewOrder> orders = new ArrayList<>();

    public HomeListAdapter(ArrayList<NewOrder> orders) {
        this.orders = orders;
    }

    @Override
    public int getCount() {
        return orders == null ? 0 : orders.size();
    }

    @Override
    public Object getItem(int position) {
        return orders.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.home_list_view_item, parent, false);
            holder = new ViewHolder();
            holder.mOrderTitle = (TextView) convertView.findViewById(R.id.order_title);
            holder.mOrderStatus = (TextView) convertView.findViewById(R.id.order_status);
            holder.mOrderCreateTime = (TextView) convertView.findViewById(R.id.order_appoint_time);
            holder.mOrderAddress = (TextView) convertView.findViewById(R.id.order_address);
            holder.mB2BLogo = (ImageView) convertView.findViewById(R.id.b2b_logo);
            holder.mStatusColor = (ImageView) convertView.findViewById(R.id.status_color);
            holder.mDistance = (TextView) convertView.findViewById(R.id.order_distance);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final NewOrder order = orders.get(position);
        holder.mOrderTitle.setText(orders.get(position).title);
        switch (order.statusTip) {
            case "已取消":
                holder.mOrderCreateTime.setBackgroundDrawable(AppData.getContext().getResources().getDrawable(R.drawable.rounder_corner_grey));
                holder.mStatusColor.setBackgroundResource(R.drawable.ic_status_cancel);
                break;
            case "停滞中":
                holder.mOrderCreateTime.setBackgroundDrawable(AppData.getContext().getResources().getDrawable(R.drawable.rounder_corner_orange));
                holder.mStatusColor.setBackgroundResource(R.drawable.ic_status_orange);
                break;
            case "已完成":
                holder.mOrderCreateTime.setBackgroundDrawable(AppData.getContext().getResources().getDrawable(R.drawable.rounder_corner_grey));
                holder.mStatusColor.setBackgroundResource(R.drawable.ic_status_cancel);
                break;
            case "待付款":
                holder.mOrderCreateTime.setBackgroundDrawable(AppData.getContext().getResources().getDrawable(R.drawable.rounder_corner_red1));
                holder.mStatusColor.setBackgroundResource(R.drawable.ic_status_red);
                break;
            case "已接订单":
                holder.mOrderCreateTime.setBackgroundDrawable(AppData.getContext().getResources().getDrawable(R.drawable.rounder_corner_blue));
                holder.mStatusColor.setBackgroundResource(R.drawable.ic_status_blue);
                break;
            case "处理中":
                holder.mOrderCreateTime.setBackgroundDrawable(AppData.getContext().getResources().getDrawable(R.drawable.rounder_corner_orange));
                holder.mStatusColor.setBackgroundResource(R.drawable.ic_status_orange);
        }
        holder.mOrderCreateTime.setText(orders.get(position).dateStatusTip);
        if(order.address2==null){
            holder.mOrderAddress.setText(order.address1);
        }else {
            holder.mOrderAddress.setText(order.address1+order.address2);
        }
        holder.mOrderStatus.setText(order.statusTip);
        holder.mOrderStatus.setTextColor(Color.parseColor(order.statusColor));
        if(order.appType.equals("1000")){
            holder.mB2BLogo.setVisibility(View.VISIBLE);
        }else {
            holder.mB2BLogo.setVisibility(View.INVISIBLE);
        }
        holder.mDistance.setText(order.distance+"米");
        return convertView;
    }


    public static class ViewHolder {
        public TextView mOrderTitle;
        public TextView mOrderStatus;
        public TextView mOrderCreateTime;
        public TextView mOrderAddress;
        public ImageView mB2BLogo;
        public ImageView mStatusColor;
        public TextView mDistance;
    }
}
